package com.taptech.doufu.ui.viewholder.personalcenter;

import android.content.Context;

import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.bean.MineNovelReviewBean;
import com.taptech.doufu.bean.MineTopicBean;
import com.taptech.doufu.bean.NovelDraftBean;
import com.taptech.doufu.bean.NovelSectionBean;
import com.taptech.doufu.bean.PersonalNoteBean;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.cartoon.CartoonBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.personalcenter.PersonalCardInfo;
import com.taptech.doufu.ui.view.personalcenter.OldPersonalNoteViewHolder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MessageViewHolderFactory extends BaseViewHolderFactory {

    // 这里必须下面一致
    static public final int ITEM_TYPE_NOVELS = 1;
    static public final int ITEM_TYPE_NOVEL_REVIEWS = 2;
    static public final int ITEM_TYPE_TYPETITLE = 3;
    static public final int ITEM_TYPE_GROUP = 4;
    static public final int ITEM_TYPE_NOTE = 5;
    static public final int ITEM_TYPE_COUNT = 6;

    public int getViewHolderType(Object object) {
        // 根据object中的类型new不同的ViewHolder
        if (object == null) {
            return -1;
        }
        // 如果全部为空则说明是在MineContentBean的getDisplayBeanList方法中制造的type_title类型数据
        MineAbstractBean bean = (MineAbstractBean) object;
        if (bean.getObject_type() == null && bean.getTopic_type() == null) {
            return ITEM_TYPE_TYPETITLE;
        }
        //判断此数据为组群信息
        else if (bean.getObject_type().equals("30")) {
            return ITEM_TYPE_GROUP;
        }
        //一篇个人日志（有可能属于绘画、coser）
        else if (bean.getObject_type().equals("41") || bean.getObject_type().equals("42") || bean.getObject_type().equals("43")) {
            return ITEM_TYPE_NOTE;
        }
        // 这是novel小说
        else if (bean.getObject_type().equals("5") && bean.getTopic_type().equals("18")) {
            return ITEM_TYPE_NOVELS;
        }
        // 这是novel_review文评
        else if (bean.getObject_type().equals("29")) {
            return ITEM_TYPE_NOVEL_REVIEWS;
        } else {
            return -1;
        }
    }

    public BaseAdapterViewHolder createViewHolder(Context context, Object object, int position) {
        BaseAdapterViewHolder viewHolder = null;
        // 根据object中的类型new不同的ViewHolder
        if (object == null) {
            return null;
        }
        switch (getViewHolderType(object)) {
            case ITEM_TYPE_NOVELS:
                viewHolder = new NovelMessageViewHolder(context, position);
                break;
            case ITEM_TYPE_NOVEL_REVIEWS:
                viewHolder = new NovelReviewMessageViewHolder(context, position);
                break;
            case ITEM_TYPE_TYPETITLE:
                viewHolder = new MessageTypeViewHolder(context, position);
                break;
            case ITEM_TYPE_NOTE:
                viewHolder = new OldPersonalNoteViewHolder(context, position);
                break;
            default:
                break;
        }
        return viewHolder;
    }

    public List<? extends MineAbstractBean> getDataBeanFromJSONArray(JSONArray jsonArray, int specialType) {
        List<MineAbstractBean> mDisplayBeanList = new ArrayList<MineAbstractBean>();
        JSONArray datas = jsonArray;

        for (int i = 0; i < datas.length(); i++) {
            try {
                if (datas.get(i) instanceof JSONObject) {
                    String object_type = null;
                    String topic_type = null;
                    String op_type = null; //我的文评类型
                    if (((JSONObject) datas.get(i)).has("object_type")) {
                        object_type = ((JSONObject) datas.get(i)).getString("object_type");
                    }
                    if (((JSONObject) datas.get(i)).has("topic_type")) {
                        topic_type = ((JSONObject) datas.get(i)).getString("topic_type");
                    }
                    if (((JSONObject) datas.get(i)).has("op_type")) {
                        op_type = ((JSONObject) datas.get(i)).getString("op_type");
                    }
                    if (object_type == null && topic_type == null) {
                        if (op_type != null && op_type.equals("29"))   //我的文评类型。不知道为啥要出现这样一个字段
                        {
                            MineNovelReviewBean bean = new MineNovelReviewBean();
                            bean.setJson2((JSONObject) datas.getJSONObject(i).get("sweep"));
                            bean.setObject_type(op_type);
                            mDisplayBeanList.add(bean);
                        }
                    } else if (object_type.equals(Constant.OBJECT_TYPE_BOOKLIST + "")) {
                        DFHomeNovelBeans bean = new DFHomeNovelBeans();
                        bean.setJson2(datas.getJSONObject(i));
                        bean.setObject_type(object_type);
                        mDisplayBeanList.add(bean);
                    }
                    //判断此数据为组群信息
                    else {
                        // 一篇个人日志（有可能属于绘画、coser
                        if (object_type.equals(Constant.OBJECT_TYPE_NOTE + "") || object_type.equals(Constant.OBJECT_TYPE_DRAW + "") || object_type.equals(Constant.OBJECT_TYPE_COS + "")) {

                            PersonalNoteBean bean = new PersonalNoteBean();
                            bean.setJson2(datas.getJSONObject(i));
                            bean.setObject_type(object_type);
                            mDisplayBeanList.add(bean);
                        }
                        // 这是topic话题
                        else if (object_type.equals(Constant.CONTENT_TYPE_TOPIC + "") && topic_type.equals("16")) {

                            MineTopicBean bean = new MineTopicBean();
                            bean.setJson2(datas.getJSONObject(i));
                            bean.setObject_type(object_type);
                            mDisplayBeanList.add(bean);
                        }
                        // 这是novel小说
                        else if (object_type.equals(Constant.CONTENT_TYPE_TOPIC + "") && topic_type.equals("18")) {

                            MineNovelBean bean = new MineNovelBean();
                            bean.setJson2(datas.getJSONObject(i));
                            bean.setObject_type(object_type);
                            mDisplayBeanList.add(bean);
                        }
                        // 这是novel_review文评
                        else if (object_type.equals(Constant.OBJECT_TYPE_SWEEP + "")) {

                            MineNovelReviewBean bean = new MineNovelReviewBean();
                            bean.setJson2(datas.getJSONObject(i));
                            bean.setObject_type(object_type);
                            mDisplayBeanList.add(bean);
                        }
                        // 这是用户
                        else if (object_type.equals(Constant.CONTENT_TYPE_USER + "")) {

                            PersonalCardInfo bean = new PersonalCardInfo();
                            bean.setJson2(datas.getJSONObject(i));
                            bean.setObject_type(object_type);
                            mDisplayBeanList.add(bean);
                        }
                        // 这是专题
                        else if (object_type.equals(Constant.OBJECT_TYPE_ALBUMS + "")) {

                            AlbumsBean bean = new AlbumsBean();
                            bean.setJson2(datas.getJSONObject(i));
                            bean.setObject_type(object_type);
                            mDisplayBeanList.add(bean);
                        }
                        // 这是小说章节
                        else if (object_type.equals("6")) {
                            NovelSectionBean bean = new NovelSectionBean();
                            bean.setJson2(datas.getJSONObject(i));
                            bean.setObject_type(object_type);
                            mDisplayBeanList.add(bean);
                        } else if (object_type.equals(Constant.OBJECT_TYPE_NOVEL_SECTION_DRAFT + "")) {
                            NovelDraftBean bean = new NovelDraftBean();
                            bean.setJson2(datas.getJSONObject(i));
                            bean.setObject_type(object_type);
                            if (((JSONObject) datas.get(i)).has("content")) {
                                JSONObject contentObject = ((JSONObject) datas.get(i)).getJSONObject("content");
                                if (contentObject.has("public")) {
                                    String publish = contentObject.getString("public");
                                    bean.getContent().setPublish(publish);
                                }
                            }
                            mDisplayBeanList.add(bean);
                        } else if (object_type.equals(Constant.OBJECT_TYPE_CARTOON + "")) {
                            CartoonBean bean = com.alibaba.fastjson.JSONObject.parseObject(datas.getJSONObject(i).toString(), CartoonBean.class);
                            mDisplayBeanList.add(bean);
                        } else {

                        }
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (specialType != 0 && mDisplayBeanList != null) {
                for (int j = 0; j < mDisplayBeanList.size(); j++)
                    mDisplayBeanList.get(j).setSpecialType(specialType);
            }
        }

        return mDisplayBeanList;
    }

    public List<? extends MineAbstractBean> getDataBeanFromJSONArray(JSONArray jsonArray) {
        return getDataBeanFromJSONArray(jsonArray, 0);
    }

    @Override
    public int getItemTypeCount() {
        // TODO Auto-generated method stub
        return ITEM_TYPE_COUNT;
    }

}
